package com.gsafety.wellsfort.controller.organization;

import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ExcelUtil;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.domain.dto.ids.ClassIdDTO;
import com.gsafety.wellsfort.domain.dto.school.ClassDTO;
import com.gsafety.wellsfort.domain.model.organization.Class;
import com.gsafety.wellsfort.domain.vo.ClassVO;
import com.gsafety.wellsfort.service.organization.IClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: wuming
 * @Date: 2020/5/1 下午3:30
 * @Description
 */
@Slf4j
@RestController
@Api(value = "班级管理", description = "班级管理接口")
@RequestMapping("/api/class")
public class ClassController {

    @Autowired
    IClassService classService;


    @ApiOperation(value = "获取班级列表")
    @PostMapping(value = "/list")
    public ApiResponse<List<Class>> list(@RequestBody ClassDTO classDTO) {
        return classService.getAllClass(classDTO);
    }

    @ApiOperation(value = "添加班级")
    @PostMapping(value = "/add")
    public ApiResponse addClass(@RequestBody ClassVO classVO) {
        return classService.addClass(classVO);
    }

    @ApiOperation(value = "删除班级")
    @PostMapping(value = "/delete")
    public ApiResponse deleteClass(@RequestBody ClassIdDTO idDTO) {
        return classService.deleteClass(idDTO.getClassId());
    }

    @ApiOperation(value = "修改班级")
    @PostMapping(value = "/update")
    public ApiResponse updateClassInfo(@RequestBody Class param) {
        return classService.updateClassInfo(param);
    }


    /**
     * 班級批量添加
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    public ApiResponse batchAdd(@RequestParam("fileName") MultipartFile file, Integer schoolId) {
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件后缀
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            // 用uuid作为文件名，防止生成的临时文件重复
            final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
            // MultipartFile to File
            file.transferTo(excelFile);
            List<ClassVO> list = ExcelUtil.getDtoList(excelFile, ClassVO.class);
            // 程序结束时，删除临时文件
            deleteFile(excelFile);
            return classService.batchAddClass(list, schoolId);
        } catch (Exception e) {
            log.error("exception={}", e);
            return ApiResponse.error(ResultCode.ERROR, e.getMessage());
        }
    }

    /**
     * 删除
     *
     * @param files
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
